package com.jtny.nytb.dataparam.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jtny.component.entity.PageData;
import com.jtny.nytb.dataparam.service.DataParamService;
import com.jtny.nytb.dataparam.vo.*;
import com.jtny.nytb.equipment.vo.dataparam.AlgorithmExcelVO;
import com.jtny.nytb.equipment.vo.excel.*;
import com.jtny.nytb.excel.utils.EasyExcelUtils;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.web.bind.annotation.RestController;
import com.jtny.component.entity.WebResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "数据参量管理", tags = "数据参量管理")
@AllArgsConstructor
@RequestMapping("/generic/dataParam")
public class DataParamController {
    @Resource
    DataParamService dataParamService;
    @ApiOperation("保存数据参量数据")
    @ApiImplicitParam(name = "configDataParamManagementVO", value = "单据编号规则数据", required = true, paramType = "query")
    @ApiOperationSupport(order = 1)
    @PostMapping("/save")
    public WebResponse<ConfigDataParamManagementVO> save(@RequestBody @Validated ConfigDataParamManagementVO configDataParamManagementVO) {
        return WebResponse.success(dataParamService.save(configDataParamManagementVO));
    }

    @ApiOperation("通过id查询参量数据")
    @ApiImplicitParam(name = "id", value = "通过id查询参量数据", required = true, paramType = "path")
    @ApiOperationSupport(order = 2)
    @GetMapping("/queryById/{id}")
    public WebResponse<ConfigDataParamManagementVO> queryById(@PathVariable("id") Long id){
        ConfigDataParamManagementVO configDataParamManagementVO = dataParamService.queryById(id);
        return WebResponse.success(configDataParamManagementVO);
    }

    @ApiOperation("更新参量数据")
    @ApiImplicitParam(name = "configDataParamManagementVO", value = "更新参量数据", required = true, paramType = "query")
    @ApiOperationSupport(order = 3)
    @PostMapping("/update")
    public WebResponse<ConfigDataParamManagementVO> update(@RequestBody @Validated ConfigDataParamManagementVO configDataParamManagementVO){
        ConfigDataParamManagementVO update = dataParamService.update(configDataParamManagementVO);
        return WebResponse.success(update);
    }

    @ApiOperation("分页查询")
    @ApiImplicitParam(name = "configDataParamManagementPageVO", value = "分页查询", required = true, paramType = "query")
    @ApiOperationSupport(order = 4)
    @PostMapping("/queryPage")
    public WebResponse<PageData<ConfigDataParamManagementVO>> queryPage(@RequestBody ConfigDataParamManagementPageVO configDataParamManagementPageVO){
        PageData<ConfigDataParamManagementVO> pageData = dataParamService.queryByPage(configDataParamManagementPageVO);
        return WebResponse.success(pageData);
    }

    @ApiOperation("删除参量数据")
    @ApiImplicitParam(name = "id", value = "分页查询", required = true, paramType = "path")
    @ApiOperationSupport(order = 5)
    @PostMapping("/deleteById/{id}")
    public WebResponse<?> deleteById(@PathVariable("id") Long id){
        dataParamService.deleteById(id);
        return WebResponse.success("删除成功");
    }

    @ApiOperation("导出excel")
    @ApiOperationSupport(order = 6)
    @PostMapping("/downloadManagement")
    public void downloadManagement(HttpServletResponse response) {
        dataParamService.downloadManagement(response);
    }

    @ApiOperation("导入模板")
    @ApiOperationSupport(order = 7)
    @PostMapping("/downloadTeamplate")
    public void downloadTeamplate(HttpServletResponse response){
        dataParamService.downloadTeamplate(response);
    }

    @PostMapping("/importParam")
    @ApiOperation(value = "导入参量")
    public WebResponse<?> importExcel(@RequestParam("file") MultipartFile file) {
        List<ExcelSheetVO> excelSheetList = addDataParamSheet();
        Map<Class<?>, List<?>> map;
        try {
            map = EasyExcelUtils.importExcelManySheet(file, excelSheetList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        this.importData(map);
        return WebResponse.success();
    }

    // 转换Map类型
    private void importData(Map<Class<?>, List<?>> map) {
        Map<String, List<?>> importData  = new HashMap<>(0);
        map.forEach((k,v)-> importData.put(k.getSimpleName(),v));
        dataParamService.importParamAndAlgorit(importData);
    }

    @PostMapping("/exportTemplate")
    @ApiOperation(value = "获取导入模板")
    public void exportTemplate(HttpServletResponse response) {
        EasyExcelUtils.exportExcelManySheet(response, "参量模板", addDataParamSheet());
    }

    public static List<ExcelSheetVO> addDataParamSheet() {
        List<ExcelSheetVO> sheetList = new ArrayList<>();
        ExcelSheetVO dataParamSheet = new ExcelSheetVO();
        dataParamSheet.setSheetName("设备模型参量");
        dataParamSheet.setClazz(ExcelDataParamVO.class);
        sheetList.add(dataParamSheet);

        ExcelSheetVO frequencySheet = new ExcelSheetVO();
        frequencySheet.setSheetName("参量频率");
        frequencySheet.setClazz(ExcelFrequencyVO.class);
        sheetList.add(frequencySheet);

        ExcelSheetVO algorithmSheet = new ExcelSheetVO();
        algorithmSheet.setSheetName("算法");
        algorithmSheet.setClazz(AlgorithmExcelVO.class);
        sheetList.add(algorithmSheet);

        ExcelSheetVO algorithmParamSheet = new ExcelSheetVO();
        algorithmParamSheet.setSheetName("算法参数");
        algorithmParamSheet.setClazz(ExcelAlgorithmParamVO.class);
        sheetList.add(algorithmParamSheet);

        ExcelSheetVO alarmSheet = new ExcelSheetVO();
        alarmSheet.setSheetName("设备告警");
        alarmSheet.setClazz(ExcelAlarmVO.class);
        sheetList.add(alarmSheet);

        return sheetList;
    }

}

